package uk.co.q3c.deplan.server.unit.domain.util;

import java.util.Calendar;
import java.util.GregorianCalendar;

import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import uk.co.q3c.deplan.client.domain.resource.IndividualResource;
import uk.co.q3c.deplan.client.domain.task.PlanDailyProfile;
import uk.co.q3c.deplan.client.domain.task.PlanProfileEntry;
import uk.co.q3c.deplan.server.unit.dao.LocalDatastoreTestCase;

@Test
public class DailyProfile_UT extends LocalDatastoreTestCase {
	PlanDailyProfile dp;
	IndividualResource ir;

	@Override
	@BeforeClass
	protected void beforeClass() {
		ir = new IndividualResource(connection);
		ir.setName("dsowerby");
	}

	@Override
	protected void beforeMethod() throws Exception {
		super.beforeMethod();
		dp = new PlanDailyProfile(ir);
	}

	@Override
	protected void afterMethod() throws Exception {
		super.afterMethod();

	}

	/**
	 * Confirm that duplicate rejected, but no exception
	 */
	public void duplicate() {
		PlanProfileEntry db1 = new PlanProfileEntry(connection, "20090802");
		PlanProfileEntry db2 = new PlanProfileEntry(connection, "20090802");
		dp.addEntry(db1);
		dp.addEntry(db2);
		Assert.assertEquals(dp.size(), 1);
		Assert.assertEquals(dp.first(), db1);
	}

	public void firstAndLast() {
		PlanProfileEntry db1 = new PlanProfileEntry(connection, "20090802");
		PlanProfileEntry db2 = new PlanProfileEntry(connection, "20090803");
		PlanProfileEntry db3 = new PlanProfileEntry(connection, "20090804");
		// add them in the "wrong" order
		dp.addEntry(db3);
		dp.addEntry(db1);
		dp.addEntry(db2);
		Assert.assertEquals(dp.first(), db1);
		Assert.assertEquals(dp.last(), db3);
	}

	public void getEntry() {
		PlanProfileEntry db1 = new PlanProfileEntry(connection, "20090802");
		PlanProfileEntry db2 = new PlanProfileEntry(connection, "20090803");
		PlanProfileEntry db3 = new PlanProfileEntry(connection, "20090804");
		// add them in the "wrong" order
		dp.addEntry(db3);
		dp.addEntry(db1);
		dp.addEntry(db2);
		Assert.assertEquals(dp.getEntry("20090802"), db1);
		Assert.assertEquals(dp.getEntry("20090803"), db2);
		Assert.assertEquals(dp.getEntry("20090804"), db3);
		Assert.assertTrue(dp.getEntry("20090802") == db1);
		// too early
		Assert.assertEquals(dp.getEntry("20090801"), null);
		// too late
		Assert.assertEquals(dp.getEntry("20090805"), null);
		Calendar cal = new GregorianCalendar();
		cal.set(2009, 07, 03, 4, 50);
		Assert.assertEquals(db2, dp.getEntry(cal.getTime()));
		cal.set(2009, 07, 03, 4, 50);
		Assert.assertEquals(db2, dp.getEntry(cal.getTime()));
	}

	public void addNull() {
		dp.addEntry(null);
		Assert.assertEquals(dp.size(), 0);
	}
}
